import java.util.HashMap;
import java.util.function.BiFunction;

public class Exam02 {
    public static void main(String[] args) {
        String str = "abcdaaaabbbcccdd";
        HashMap<String, Integer> hashMap1 = new HashMap<>();
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);

            //第一种方法
            hashMap1.compute(c + "", new BiFunction<String, Integer, Integer>() {
                @Override
                public Integer apply(String key, Integer value) {
//                    System.out.println(value);
                    return value == null ? 1 : value + 1;
                }
            });

            //第二种方法
            if (hashMap1.containsKey(c)) {
                hashMap1.put(c + "", hashMap1.get(c) + 1);
            } else {
                hashMap1.put(c + "", 1);
            }
        }
        System.out.println(hashMap1);
    }
}
